<?php
namespace Home\Controller;
use Think\Controller;
class ZMController extends BaseController {
    protected function addLog($modelname,$opertype,$id,$desc,$olddata){
        $userid = $_SESSION['user']['userid'];
        $model = M('Operlog');
        $data['hid'] = $_SESSION['user']['hid'];
        $data['userid'] =  $userid;
        $data['modename'] = $modelname;
        $data['opertype'] = $opertype;
        $data['id'] = $id;
        $data['memo'] = $desc;
        $data['olddata'] = $olddata;
        $model->add($data);
    }

    protected function addD($hid,$oper,$opername){
        $db_host = C('DDWK_DB_HOST');
        $db_user = C('DDWK_DB_USER');
        $db_pwd = C('DDWK_DB_PWD');
        $db_name = C('DDWK_DB_NAME');
        $tname = "ddwk_".$opername;
        $conn = mysql_connect($db_host,$db_user,$db_pwd);
        if (!empty($conn)) {
            mysql_select_db($db_name,$conn);
            mysql_query("SET NAMES 'UTF8'",$conn);
            $resddwk = mysql_query($oper,$conn);
            if (!empty($resddwk)) {
                $Model = M("$opername");
                $Model->query("update $tname set flag = 0 where id = $hid");
                }
                mysql_close();
        }
    }

    protected function Conddwk($opername,$oper,$id){
        $db_host = C('DDWK_DB_HOST');
        $db_user = C('DDWK_DB_USER');
        $db_pwd = C('DDWK_DB_PWD');
        $db_name = C('DDWK_DB_NAME');
        $tname = "ddwk_".$opername;
        $conn = mysql_connect($db_host,$db_user,$db_pwd);
        if (!empty($conn)) {
            mysql_select_db($db_name,$conn);
            mysql_query("SET NAMES 'UTF8'",$conn);
            $resddwk=mysql_query($oper,$conn);
        }
        mysql_close();
        $Model = M("$opername");
        $data = $Model->create();
        $data['flag'] = 0;
        if(!empty($resddwk)) {
        $res = $Model->where("id=$id")->save($data);
        }
    }
    
    /**
    * 获取应用的路径
    * 
    */
    protected function getAppPath(){
        $app_url = dirname(__APP__);
        $app_url = str_replace("\\","/",$app_url);
        if(substr($app_url, -1, 1)!='/') $app_url .= '/';
        return $app_url;
    }
    
    protected function ajaxRetArray($result,$count=null){
        if($result==null){
            $result = array();
        }
        if($count==null){
            $count = count($result);
        }
        $this->ajaxReturn($result,$count,true);
    }
    
    protected function ajaxReturnWithMeta($data,$msg,$success,$metadata=null) {
        $info['data']   =   $data;
        $info['mesage']=   $msg;
        $info['success'] =  $success;
        if($metadata) $info['metaData'] = $metadata;
        $data           =   $info;
        // 返回JSON数据格式到客户端 包含状态信息
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($data));
    }
    /*
    protected function ajaxReturn($data,$msg=''){
        parent::ajaxReturn($data,$message,true);
    }
    */
}